package com.example.bigevent.controller;

import com.example.bigevent.pojo.Article;
import com.example.bigevent.pojo.PageBean;
import com.example.bigevent.pojo.Result;
import com.example.bigevent.service.ArticleService;
import com.example.bigevent.validation.ValidationGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 新增文章
     */
    @PostMapping
    @PreAuthorize("hasAuthority('article:add')")
    public Result add(@RequestBody @Validated(ValidationGroup.AddArticle.class) Article article){
        articleService.add(article);
        return Result.success();
    }

    /**
     * 查询文章列表
     */
    @GetMapping
    @PreAuthorize("hasAuthority('article:query')")
    public Result<PageBean<Article>> list(Integer pageNum,Integer pageSize,Article article){
        PageBean<Article> pageBean=articleService.list(pageNum,pageSize,article);
        return Result.success(pageBean);
    }

    /**
     * 查询后台文章列表
     */
    @GetMapping("/sys/list")
    @PreAuthorize("hasAuthority('system:article:query')")
    public Result<PageBean<Article>> sysList(Integer pageNum,Integer pageSize,Article article){
        PageBean<Article> pageBean=articleService.sysList(pageNum,pageSize,article);
        return Result.success(pageBean);
    }

    /**
     * 更新文章，更新后台文章
     */
    @PutMapping
    @PreAuthorize("hasAnyAuthority('article:update','system:article:update')")
    public Result update(@RequestBody @Validated(ValidationGroup.UpdateArticle.class) Article article){
        articleService.update(article);
        return Result.success();
    }

    /**
     * 获取文章详细信息
     */
    @GetMapping("/detail")
    @PreAuthorize("hasAuthority('article:query')")
    public Result<Article> detail(@RequestParam("id") Integer id){
        Article article=articleService.findById(id);
        return Result.success(article);
    }

    /**
     * 删除文章，删除后台文章
     */
    @DeleteMapping
    @PreAuthorize("hasAnyAuthority('article:delete','system:article:delete')")
    public Result delete(@RequestParam("id") Integer id){
        articleService.deleteById(id);
        return Result.success();
    }
}
